<?php

defined("IN_CA") or die();

$this->template = new TemplateHandler("adm_statistics.php", ADM_TEMPLATE_DIR);

$lastmonth			= mktime(0, 0, 0, date("m")-1	, date("d")		, date("Y"));
$lastweek			= mktime(0, 0, 0, date("m")		, date("d")-7	, date("Y"));
#$yesterday			= mktime(0, 0, 0, date("m")		, date("d")-1	, date("Y"));
$today				= mktime(0, 0, 0, date("m")		, date("d")		, date("Y"));
$tomorrow			= mktime(0, 0, 0, date("m")		, date("d")+1	, date("Y"));

# A hell of queries

$q_total			= $this->db->query("SELECT count(*) FROM `clicks`");
$q_unique			= $this->db->query("SELECT COUNT(DISTINCT ip) FROM `clicks`");

$q_lastmonth_total	= $this->db->query("SELECT count(*) FROM `clicks` WHERE `timestamp` BETWEEN '" . date("Y-m-d", $lastmonth) . "' AND '" . date("Y-m-d", $today) . "'");
$q_lastmonth_unique	= $this->db->query("SELECT COUNT(DISTINCT ip) FROM `clicks` WHERE `timestamp` BETWEEN '" . date("Y-m-d", $lastmonth) . "' AND '" . date("Y-m-d", $today) . "'");

$q_lastweek_total	= $this->db->query("SELECT count(*) FROM `clicks` WHERE `timestamp` BETWEEN '" . date("Y-m-d", $lastweek) . "' AND '" . date("Y-m-d", $today) . "'");
$q_lastweek_unique	= $this->db->query("SELECT COUNT(DISTINCT ip) FROM `clicks` WHERE `timestamp` BETWEEN '" . date("Y-m-d", $lastweek) . "' AND '" . date("Y-m-d", $today) . "'");

$q_today_total		= $this->db->query("SELECT count(*) FROM `clicks` WHERE `timestamp` BETWEEN '" . date("Y-m-d", $today) . "' AND '" . date("Y-m-d", $tomorrow) . "'");
$q_today_unique		= $this->db->query("SELECT COUNT(DISTINCT ip) FROM `clicks` WHERE `timestamp` BETWEEN '" . date("Y-m-d", $today) . "' AND '" . date("Y-m-d", $tomorrow) . "'");

# A hell of fetches

$p_total			= $this->db->fetchArray($q_total, MYSQL_NUM);
$p_unique			= $this->db->fetchArray($q_unique, MYSQL_NUM);

$p_lastmonth_total	= $this->db->fetchArray($q_lastmonth_total, MYSQL_NUM);
$p_lastmonth_unique	= $this->db->fetchArray($q_lastmonth_unique, MYSQL_NUM);

$p_lastweek_total	= $this->db->fetchArray($q_lastweek_total, MYSQL_NUM);
$p_lastweek_unique	= $this->db->fetchArray($q_lastweek_unique, MYSQL_NUM);

$p_today_total		= $this->db->fetchArray($q_today_total, MYSQL_NUM);
$p_today_unique		= $this->db->fetchArray($q_today_unique, MYSQL_NUM);

# Finally, assignment.

$this->template->assign(
	"clicks",
	array(
		"total" => $p_total[0],
		"unique" => $p_unique[0],
		"lastmonth_total" => $p_lastmonth_total[0],
		"lastmonth_unique" => $p_lastmonth_unique[0],
		"lastweek_total" => $p_lastweek_total[0],
		"lastweek_unique" => $p_lastweek_unique[0],
		"today_total" => $p_today_total[0],
		"today_unique" => $p_today_unique[0],
	)
);

?>